Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux'});
Ext.require([
             'Ext.grid.*',
             'Ext.data.*',
             'Ext.ux.grid.FiltersFeature',
             'Ext.toolbar.Paging',
             'Ext.ux.ajax.JsonSimlet',
             'Ext.ux.ajax.SimManager',
             'Ext.ux.grid.Printer'        
]);
Ext.define('TS.controller.ProductTransferBills',{
	extend: 'Ext.app.Controller',
	
	models:['common.BillAmountModel','common.BillExtendModel','common.InventoryAddModel',
	'inv.ProductTransferBillModel','inv.ProductTransferBillDetailModel','inv.ProductTransferBillMDModel'],
	stores:['inv.ProductTransferBillStore','inv.ProductTransferBillDetailStore','inv.ProductTransferBillMDStore'],
	views:['inv.ProductTransferBillList','inv.ProductTransferBillEditor'],
	init:function() {
		this.control({
			'combinebilllist button[action=search]':{click:this.search},
			'combinebilllist button[action=advsearch]':{click:this.advsearch},
			'combinebilllist button[action=createBill]':{click:this.createBill},
			'combinebilllist button[action=editBill]':{click:this.editBill},
			'combinebilllist button[action=deleteBill]':{click:this.deleteBill},
			'combinebilllist button[action=archiveBill]':{click:this.archiveBill},
			'combinebilllist button[action=unArchiveBill]':{click:this.unArchiveBill},
			'combinebilllist button[action=printBill]':{click:this.printBill},
			'combinebilllist button[action=exportBill]':{click:this.exportBill},
			'combinebilllist grid':{itemdblclick:this.dbclickBill},
			'combinebilllist combobox':{select:this.groupChange}
		});
	},
	deleteBill:function(button){
		var grid = button.up('grid');
		var records=grid.getSelectionModel().getSelection();
		if(records && records.length == 1) {
			var bill=records[0];
			if(bill && bill.get('archived')) {
				Ext.Msg.show({title:'ThinkGsp',msg:'单据已经归档,不能删除!',buttons:Ext.Msg.OK,icon:Ext.Msg.WARNING
				});
				return;
			};
	    	Ext.Msg.confirm('ThinkGSP','真的要删除选择的单据吗?',function(b){
				if(b=='yes'){
					Ext.Ajax.request({
						    url:'combine/delete.do',
						    method : 'POST',
						    timeout :3000,
						    params:{billId:bill.get('billId')},
						    success:function(response){		
						    	var rs=Ext.decode(response.responseText);
						    	if(rs.success==true){
							    	if(rs.success==true){
							    		grid.getStore().remove(bill);
		 								grid.getView().refresh();
								    }
							    }
						    	showExtReturnMsg(response);
						    },
						    scope:this
			    	});
				}
			});
		}else{
			Ext.Msg.show({title:'ThinkGSP',msg:'请选择一条单据记录!',buttons:Ext.Msg.OK,icon:Ext.Msg.WARNING});
		}
	},
	archiveBill:function(button){
		var grid = button.up('grid');
		var records=grid.getSelectionModel().getSelection();
		if(records && records.length == 1) {
			var bill=records[0];
			if(bill.get('billId') || bill.get('billId') != 0) {
	 			if(bill.get('archived')) {
					Ext.Msg.show({title:'ThinkGsp',msg:'单据已经归档!',buttons:Ext.Msg.OK,icon:Ext.Msg.WARNING});
					return;
				}
				Ext.Ajax.request({
						    url:'combine/archive.do',
						    method : 'POST',
						    timeout :3000,
						    params:{billId:bill.get('billId')},
						    success:function(response){		
						    	var rs=Ext.decode(response.responseText);
						    	if(rs.success==true){
							    	if(rs.success==true){
							    		bill.set('archived',true);
								    	grid.getView().refresh();
								    }
							    }
						    	showExtReturnMsg(response);
						    },
						    scope:this
			    });
			}
		}else{
			Ext.Msg.show({title:'ThinkGSP',msg:'请选择一条单据记录!',buttons:Ext.Msg.OK,icon:Ext.Msg.WARNING});
		}
	},
	unArchiveBill:function(button){
		var grid = button.up('grid');
		var records=grid.getSelectionModel().getSelection();
		if(records && records.length == 1) {
			var bill=records[0];
			if(bill.get('billId') || bill.get('billId') != 0) {
	 			if(!bill.get('archived')) {
					Ext.Msg.show({title:'ThinkGsp',msg:'单据未归档!',buttons:Ext.Msg.OK,icon:Ext.Msg.WARNING});
					return;
				}
				Ext.Ajax.request({
						    url:'combine/unArchive.do',
						    method : 'POST',
						    timeout :3000,
						    params:{billId:bill.get('billId')},
						    success:function(response){		
						    	var rs=Ext.decode(response.responseText);
						    	if(rs.success==true){
							    	if(rs.success==true){
							    		bill.set('archived',false);
								    	grid.getView().refresh();
								    }
							    }
						    	showExtReturnMsg(response);
						    },
						    scope:this
			    });
			}
		}else{
			Ext.Msg.show({title:'ThinkGSP',msg:'请选择一条单据记录!',buttons:Ext.Msg.OK,icon:Ext.Msg.WARNING});
		}		
	},
	printBill:function(button){
		
	},
	exportBill:function(button){
		
	},
	dbclickBill:function(grid,record){
		if(grid.getSelectionModel().getSelection() &&
				grid.getSelectionModel().getSelection().length > 0) {
			this.openBill(record);
		}
	},
	editBill:function(button){
		var grid = button.up('grid');
		if(grid.getSelectionModel().getSelection() &&
				grid.getSelectionModel().getSelection().length > 0) {
			this.openBill(grid.getSelectionModel().getSelection()[0]);
		}
	},
	openBill:function(record) {
		var billId = record.get('billId');
		var store = Ext.create('Ext.data.Store',{
			model: 'TS.model.inv.ProductTransferBillModel',
			proxy:{
				url:'combine/getBill.do',
				type:'ajax',
				reader:{
					type:'json',
					root:''
				}
			},
			autoLoad:false
		});
		store.load({
			params:{billId:billId},
			callback:function(records, operation, success) {
				if(records && records.length > 0) {
					var bill = records[0];
					var billForm = Ext.create('TS.view.inv.ProductTransferBillEditor',{closable : true,bill:bill});
					var main = Ext.getCmp("syscontent");
					var panel = main.add(billForm);
					main.setActiveTab(panel);
				}
			},
			scope:this
		});
	},
	createBill:function(button){
		var billForm = Ext.create('TS.view.inv.ProductTransferBillEditor',{closable : true});
		var main = Ext.getCmp("syscontent");
		var panel = main.add(billForm);
		main.setActiveTab(panel);
	},
	search:function(button){
		if(button == null) {
			button = Ext.ComponentQuery.query('combinebilllist button[action=search]')[0];
		}
		var begDateField = button.up('panel').down('datefield[name=begDate]');
		var endDateField = button.up('panel').down('datefield[name=endDate]');
		var custId = button.up('panel').down('hiddenfield[name=custId]');
		var grid = button.up('panel[alias=widget.combinebilllist]').down('gridpanel');
		grid.store.on("beforeload",function(){
	        Ext.apply(grid.store.proxy.extraParams, {begDate:begDateField.getRawValue(),endDate:endDateField.getRawValue()});
	    });
		grid.store.loadPage(1,{
			params:{begDate:begDateField.getRawValue(),endDate:endDateField.getRawValue()},
			callback:function(records, options, success){
				//this.loadMasterDetail(records,button.up('panel[alias=widget.combinebilllist]').down('tabpanel').items.items[1])
				if(options.exception){
					showStoreLoadException(options);						
				}
			},
			scope:this			
		});
		var st=Ext.create('TS.store.inv.ProductTransferBillStore');
		st.load({
			url: 'combine/queryNoPaging.do',
			params:{begDate:begDateField.getRawValue(),endDate:endDateField.getRawValue()},
			callback:function(records, options, success){
				if(options.exception){
					showStoreLoadException(options);						
				}else{

					this.loadMasterDetail(records,button.up('panel[alias=widget.combinebilllist]').down('tabpanel').items.items[1]);
				}
			},
			scope:this	
		});
	},
	loadMasterDetail:function(records,detailGrid){
		var mdrecords=[];
		Ext.Array.each(records,function(record){
				record.details().each(function(detail){
					var md=Ext.create('TS.model.inv.ProductTransferBillMDModel');
					md.set('billId',record.get('billId'));
					md.set('billCode',record.get('billCode'));
					md.set('billName',record.get('billName'));
					md.set('billDate',record.get('billDate'));
					md.set('fyear',record.get('fyear'));
					md.set('period',record.get('period'));
					md.set('archived',record.get('archived'));
					md.set('remark',record.get('remark'));
					md.set('id',detail.get('id'));
					md.set('quantity',detail.get('quantity'));
					md.set('taxRate',detail.get('taxRate'));
					md.set('price',detail.get('price'));
					md.set('direct',detail.get('direct'));
					md.set('amount',detail.get('amount'));
					md.set('settlePrice',detail.get('settlePrice'));
					md.set('costPrice',detail.get('costPrice'));
					md.set('productCode',detail.getproduct().get('productCode'));
					md.set('productName',detail.getproduct().get('productName'));
					md.set('packSpec',detail.getproduct().get('packSpec'));
					md.set('manuf',detail.getproduct().get('manuf'));
					md.set('unit',detail.getproduct().get('unit'));
					md.set('batchNo',detail.getbatchNo().get('batchNo'));
					md.set('manufDate',detail.getbatchNo().get('manufDate'));
					md.set('expDate',detail.getbatchNo().get('extDate'));
					md.set('dremark',detail.get('remark'));
					md.set('deptName',record.getdept().get('deptName'));
					md.set('empName',record.getemp().get('empName'));
					md.set('makerName',record.getmaker().get('empName'));
					mdrecords.push(md);
				});
				
			}
		);
		var cbo=detailGrid.down('combobox');
		var gn=cbo.getValue();
		if('null'==gn)
			detailGrid.store.clearGrouping();
		else
			detailGrid.store.group(gn);
		detailGrid.store.loadRecords(mdrecords);
		detailGrid.store.commitChanges();
		detailGrid.getView().refresh();
	},
	advsearch:function(button){
		
	},
	groupChange:function(combo, records, eOpts){
		var grid=combo.up('grid');
		var gn=records[0].get('groupName');
		if('null'==gn)
			grid.store.clearGrouping();
		else
			grid.store.group(gn);
		grid.getView().refresh();
		
	}
});